﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;

namespace YArchitech.HYSpace.SpaceManager.Manager
{
	internal class SpaceManager
	{
		public SpaceManager(ExternalCommandData data, Dictionary<int, List<Space>> spaceData)
		{
			this.m_commandData = data;
			this.m_spaceDictionary = spaceData;
		}

		public List<Space> GetSpaces(Level level)
		{
			return this.m_spaceDictionary[level.Id.IntegerValue];
		}

		public void CreateSpaces(Level level, Phase phase)
		{
			try
			{
				ICollection<ElementId> collection = this.m_commandData.Application.ActiveUIDocument.Document.Create.NewSpaces2(level, phase, this.m_commandData.Application.ActiveUIDocument.Document.ActiveView);
				foreach (ElementId elementId in collection)
				{
					Space space = this.m_commandData.Application.ActiveUIDocument.Document.GetElement(elementId) as Space;
					if (space != null)
					{
						this.m_spaceDictionary[level.Id.IntegerValue].Add(space);
					}
				}
				if (collection == null || collection.Count == 0)
				{
					TaskDialog.Show("Revit", "There is no enclosed loop in " + level.Name);
				}
			}
			catch (Exception ex)
			{
				TaskDialog.Show("Revit", ex.Message);
			}
		}

		private ExternalCommandData m_commandData;

		private Dictionary<int, List<Space>> m_spaceDictionary;
	}
}
